package com.example.bodycontrolmoduledomain

import com.example.bodycontrolmodule.impl.ITailGateRepository
import com.example.bodycontrolmodule.impl.TailGateRepository
import com.example.datacore.DomainScope
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.async
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import javax.inject.Inject


/**
 * Ui 数据类
 * */
data class TailGateUiState(
    val openBtnEnable:Boolean = false,
    val closeBtnEnable:Boolean = false,
    val tailGatePos:Int = 50,
)

class TailGateCases @Inject constructor(
    @TailGateRepository
    private val tailGateRepository: ITailGateRepository,
    @DomainScope
    private val domainScope: CoroutineScope
) {
    fun openTailGate() = domainScope.async {
        tailGateRepository.changeTailGateState(true)
    }

    fun closeTailGate() =domainScope.async{
        tailGateRepository.changeTailGateState(false)
    }

    fun setTailGatePos(progress: Int) =domainScope.async{
        tailGateRepository.changeTailGatePos(progress)
    }

    private val _mutableUiStateFlow: MutableStateFlow<TailGateUiState> =
        MutableStateFlow(TailGateUiState())

    //export to ui layer
    val tailGateStateFlow: StateFlow<TailGateUiState> = _mutableUiStateFlow.asStateFlow()


    init {
        domainScope.launch {
            tailGateRepository.getTailGateState().collect {
                val newState = _mutableUiStateFlow.value.copy(

                )
                _mutableUiStateFlow.emit(newState)
            }
        }
    }

}



